require(["jquery",'public','weui'],function($,public,weui) {
    //public.headertitle('个人信息');
    //public.footer(1);
    public.allocator();

    function getData(){
    	$.ajax({
            data: {token: public.getSession('token')},
	        type: 'post',
	        dataType: 'json',
	        url: MODULE+'Index/personal',	        
	    }).success(function (res) {
	        console.log(res);
            if(res.code == 0){
    	        $('[name="name"]').val(res.data.user_username);
    	        $('.userphone').html(res.data.user_phone);
    	        $('[name="email"]').val(res.data.user_email);
    	        //$('[name="password"]').val(res.data.user_password);
    	        $('[name="address"]').val(res.data.user_area);
    	        $('[name="shopname"]').val(res.data.user_shop);
    	        $('[name="shoplogo"]').val(res.data.user_picture_path);
    	        $('.weui-uploader__input-box').hide();
    	        $('.choose_box').html('<div class="choose_img_item" style="background-image:url('+PUBLIC+res.data.user_picture_path+')"><a class="choose_img_item_delete" href="#"></a></div>');
            }
	    }).fail(function (jqXHR, textStatus, errorThrown) {});
    }

    getData();

    //页面加载完毕
    public.loaded();

	var tmpl = '<div class="choose_img_item" style="background-image:url(#url#)"><a class="choose_img_item_delete" href="#"></a></div>';
    var $uploaderInput = $(".weui-uploader__input");
    var $gallery = $("#gallery");
    var $galleryImg = $("#galleryImg");
    $uploaderInput.on("change", function(e){
    	var $this = $(this);
    	var $uploaderFiles = $this.parent().siblings('.choose_box');
    	var length = $this.data('length') || 1;
        var src, url = window.URL || window.webkitURL || window.mozURL, files = e.target.files;
        for (var i = 0, len = files.length; i < len; ++i) {
            var file = files[i];
            if(file.type.search(/image/) < 0){
                $.alert('请选择图片');
                return false;
            }
            if (url) {
                src = url.createObjectURL(file);
            } else {
                src = e.target.result;
            }
            $uploaderFiles.append($(tmpl.replace('#url#', src)));
            if($uploaderFiles.find('.choose_img_item').size() >= length){
            	$this.parent().hide();
            	return false;
            }
        }
    });

    $('.choose_box').on('click','.choose_img_item_delete',function(event){
    	event.preventDefault();
    	event.stopPropagation();
    	var $this = $(this);
    	$this.closest('.choose_box').siblings('.weui-uploader__input-box').show();
    	$this.parent().remove();
    });

    $('.choose_box').on("click", ".choose_img_item", function(){
        $galleryImg.attr("style", this.getAttribute("style"));
        $gallery.fadeIn(100);
    });
    $gallery.on("click", function(){
        $gallery.fadeOut(100);
    });


    $('.save_box').on('click','.save',function(event){
    	event.preventDefault();
        var email = $('[name="email"]').val().trim();
        var address = $('[name="address"]').val().trim();
        //var shopname = $('[name="shopname"]').val().trim();
        var email_rep = /^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/;
        if(address != '' && email_rep.test(email)){
    	   $('#upload_form').append('<input class="hide" name="token" value="'+public.getSession('token')+'">').submit();
        }else{
            $.alert('信息不完整');
        }
    });

    $('#upload_frame_1').unbind("load").load(function() {
        $.alert("修改成功",function(){
            window.location.replace(MODULE + 'PublicJump/personal');
        });
		/*var contents = $(this).contents().get(0);
		var data = $(contents).find('body').text();
        console.log(data);
		try{
            data = window.eval('(' + data + ')');
        }catch(e){
            if(data != ''){
                $.alert("修改失败");
            }
        }
        if(data == ''){
            $.confirm("你要离开本页么?", function() {
                window.history.go(-1);
            }, function() {
                //...
            });
            return false;
        }
        console.log(data);
        if(data.code == 0){
            $.alert("修改成功");
        }else if(data.code == 1){
            $.alert("修改失败");
        }*/
	});

})